package me.yuzho.study.concurrency;

/** 使用synchronized 关键字简单实现互斥量
 * @author yzhou-citrix
 *
 */
public class SynchronizedEvenGenerator extends EvenGenerator {

    private int currentEvenValue = 0;
    
    @Override
    public synchronized int next() {
        ++currentEvenValue; //递增不是原子性操作，如果不保护操作，即使单一的递增也不安全
        Thread.yield();
        ++currentEvenValue;
        return currentEvenValue;
    }
    
    public static void main(String[] args) {
        EvenChecker.test(new SynchronizedEvenGenerator(),10);
    }
}
